package com.ruoyi.manager.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.manager.domain.SoldierArrangeInfo;
import com.ruoyi.manager.domain.SoldierBaseInfo;
import com.ruoyi.manager.mapper.SoldierBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.mapper.SoldierEmploymentInfoMapper;
import com.ruoyi.manager.domain.SoldierEmploymentInfo;
import com.ruoyi.manager.service.ISoldierEmploymentInfoService;

/**
 * 退役军人_就业创业Service业务层处理
 *
 * @author Allen
 * @date 2021-08-30
 */
@Service
public class SoldierEmploymentInfoServiceImpl extends ServiceImpl<SoldierEmploymentInfoMapper, SoldierEmploymentInfo> implements ISoldierEmploymentInfoService {
    @Autowired
    private SoldierEmploymentInfoMapper soldierEmploymentInfoMapper;
    @Autowired
    private SoldierBaseInfoMapper soldierBaseInfoMapper;

    /**
     * 查询退役军人_就业创业
     *
     * @param id
     *         退役军人_就业创业主键
     *
     * @return 退役军人_就业创业
     */
    @Override
    public SoldierEmploymentInfo selectSoldierEmploymentInfoById(Long id) {
        return soldierEmploymentInfoMapper.selectSoldierEmploymentInfoById(id);
    }

    /**
     * 查询退役军人_就业创业列表
     *
     * @param soldierEmploymentInfo
     *         退役军人_就业创业
     *
     * @return 退役军人_就业创业
     */
    @Override
    public List<SoldierEmploymentInfo> selectSoldierEmploymentInfoList(SoldierEmploymentInfo soldierEmploymentInfo) {
        List<SoldierEmploymentInfo> employmentInfoList = soldierEmploymentInfoMapper.selectSoldierEmploymentInfoList(soldierEmploymentInfo);
        List<Long> soldierIdList = employmentInfoList.stream().map(SoldierEmploymentInfo::getSoldierId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(soldierIdList)) {
            Map<Long, SoldierBaseInfo> baseInfoMap = soldierBaseInfoMapper.selectList(new QueryWrapper<SoldierBaseInfo>().lambda().in(SoldierBaseInfo::getId, soldierIdList)).stream().collect(Collectors.toMap(SoldierBaseInfo::getId, y -> y));
            employmentInfoList.forEach(x -> {
                x.setSoldierName(CollectionUtil.isEmpty(baseInfoMap) ? "" : baseInfoMap.get(x.getSoldierId()) == null ? "" : baseInfoMap.get(x.getSoldierId()).getName());
                x.setIdCard(CollectionUtil.isEmpty(baseInfoMap) ? "" : baseInfoMap.get(x.getSoldierId()) == null ? "" : baseInfoMap.get(x.getSoldierId()).getIdNumber());
            });
        }
        return employmentInfoList;
    }

    /**
     * 新增退役军人_就业创业
     *
     * @param soldierEmploymentInfo
     *         退役军人_就业创业
     *
     * @return 结果
     */
    @Override
    public int insertSoldierEmploymentInfo(SoldierEmploymentInfo soldierEmploymentInfo) {
        return soldierEmploymentInfoMapper.insertSoldierEmploymentInfo(soldierEmploymentInfo);
    }

    /**
     * 修改退役军人_就业创业
     *
     * @param soldierEmploymentInfo
     *         退役军人_就业创业
     *
     * @return 结果
     */
    @Override
    public int updateSoldierEmploymentInfo(SoldierEmploymentInfo soldierEmploymentInfo) {
        return soldierEmploymentInfoMapper.updateSoldierEmploymentInfo(soldierEmploymentInfo);
    }

    /**
     * 批量删除退役军人_就业创业
     *
     * @param ids
     *         需要删除的退役军人_就业创业主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierEmploymentInfoByIds(Long[] ids) {
        return soldierEmploymentInfoMapper.deleteSoldierEmploymentInfoByIds(ids);
    }

    /**
     * 删除退役军人_就业创业信息
     *
     * @param id
     *         退役军人_就业创业主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierEmploymentInfoById(Long id) {
        return soldierEmploymentInfoMapper.deleteSoldierEmploymentInfoById(id);
    }
}
